package com.quanquan.parking.ketop.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.quanquan.parking.ketop.entity.UserDisCount;
import com.quanquan.parking.ketop.repository.UserDisCountRepository;
import com.quanquan.parking.ketop.request.UserDisCountRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.DateUtils;

/**
 * @ClassName:UserDisCountService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/12/30 10:25
 * @Version 1.0
 */
@Service
public class UserDisCountService {

    @Autowired
    private UserDisCountRepository userDisCountRepository;

    /**
     * 查询会员优惠（前端用）
     */
    public UserDisCount userDisCountFront() {
        return userDisCountRepository.findUserDisCountByStatus(DateUtils.currentDate());
    }

    /**
     * 查询会员优惠（后台用）
     */
    public UserDisCount userDisCountBack() {
        return userDisCountRepository.findUserDisCountBack();
    }

    /**
     * 更新会员优惠（后台用）
     */
    public boolean updateDisCountBack(UserDisCountRequest userDisCountRequest) {
        UserDisCount userDisCountBack = userDisCountRepository.findUserDisCountBack();
        BeanUtil.copyProperties(userDisCountRequest, userDisCountBack, true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        userDisCountBack.setUpdateTime(DateUtils.currentDate());
        userDisCountBack.setStartTime(DateUtils.dateToString(DateUtils.stringToDate(userDisCountRequest.getStartTime(),"yyyy-MM-dd HH:mm"),"yyyy-MM-dd HH:mm:ss"));
        userDisCountBack.setEndTime(DateUtils.dateToString(DateUtils.stringToDate(userDisCountRequest.getEndTime(),"yyyy-MM-dd HH:mm"),"yyyy-MM-dd HH:mm:ss"));
        userDisCountRepository.saveAndFlush(userDisCountBack);
        return true;
    }

}
